$(function() {

//    log = console.log;

    $.validator.addMethod("parseDate", function(value, element) {

        try {
            $.datepicker.parseDate('dd.mm.yy', value);
        } catch (e) {
            return false;
        }
        return true;
    }, $.datepicker._defaults.dateFormat);

    $('#doreservation').validate({
        errorElement: "em",
        rules: {
            agree: "required",
            birth_date: {
                required: true,
                parseDate: true
            }
        },
        messages: {
            agree: "***Please accept general terms!"
        },
        submitHandler: function(form) {

            form.submit();

        }
    });

    // Birt date
    var img = API.Data.base_url + "assets/img/calendar/calendar.png";
    $('#birth_date').datepicker({
        dateFormat: 'dd.mm.yy',
        showOn: 'both',
        buttonImage: img,
        buttonImageOnly: true,
        nextText: '&raquo;',
        prevText: '&laquo;',
        dayNamesMin: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
        showOtherMonths: true,
        firstDay: 1, // Start with Monday,
        changeMonth: true,
        changeYear: true,
        defaultDate: '-25Y',
        maxDate: new Date(),
        yearRange: '-100:+0'
    })

    show_advanced_payment = function() {

        $("#calculation").mask(API.Data.lang['calculating'] + "...");

        var value = $(this).val();

        function action_delay() {

            $.scrollTo('#calculation', 500);

            if (value === '30% now and 70% balance upon arrival' || value === '30% now and 70% balance 10 days before arrival') {
                $('.advance_payment').show();
            } else {
                $('.advance_payment').hide();
            }

            $("#calculation").unmask();
        }

        var randomnumber = 401 + Math.floor(Math.random() * 501);

        window.setTimeout(function() {
            action_delay();
        }, randomnumber);
//        return false;

    }

    $('input[name="payment_options"]').on('change', show_advanced_payment);


});


